# -*- coding: utf-8 -*-
from time import gmtime, strftime

import scrapy

from tianya.items import TianyaItem, TianyaImgItem


class MyspiderSpider(scrapy.Spider):
    name = 'myspider'
    allowed_domains = ['http://bbs.tianya.cn']
    start_urls = ['http://bbs.tianya.cn/list-free-1.shtml']

    def parse(self, response):
        # filename = response.url.split("/")[-2] + "-" + strftime("%Y-%m-%d %H:%M:%S", gmtime()) + ".html"
        # with open(filename, 'wb') as f:
        #     f.write(response.body)

        # for td in response.xpath('//td[@class="td-title faceblue"]'):
        #     item = TianyaItem()
        #     item['title'] = td.xpath('a/text()').extract()
        #     item['link'] = td.xpath('a/@href').extract()[0]
        #     yield item

        # 图片
        for img in response.xpath('//img[@class="pic"]'):
            item = TianyaImgItem()
            item['image_urls'] = img.xpath('@src').extract()
            item['images'] = img.xpath('@src').extract()
            yield item

        pass
